package com.example.mybutton;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;

import java.io.File;
import java.lang.reflect.Method;

public class SkinManager {
	private final static String APK_PATH = "/vendor/operator/app/GalaPublicResource";
	private static String APK_ABSOLUTE_PATH = "";
	private Resources mResources;
	private AssetManager mAssetManager;
	private PackageInfo mPackageInfo;	
	private static SkinManager mInstance = null;
	public static SkinManager getInstance() {
		if(mInstance == null) {
			synchronized (SkinManager.class) {
				if(mInstance == null) {
					mInstance = new SkinManager();
				}
				
			}
		}
		return mInstance;
	}
	/**
	 * 初始化
	 * @param context 上下文
	 */
	public void init(Context context) {

        try {
        	mAssetManager = createAssetManager();
            mResources = createResources(context, mAssetManager);
            initPackageInfo(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	/**
	 * 判断APK资源是否存在
	 * @return
	 */
	public boolean checkApkIsExits() {
        File file = new File(APK_PATH);
        File[] skinFile = file.listFiles();
        if (skinFile == null || skinFile.length == 0) {
            return false;
        }
        APK_ABSOLUTE_PATH = skinFile[0].getAbsolutePath();
        return true;
	}
    private AssetManager createAssetManager() {
        try {
            AssetManager assetManager = AssetManager.class.newInstance();
            Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
            addAssetPath.setAccessible( true);
            addAssetPath.invoke(assetManager, APK_ABSOLUTE_PATH);
            return assetManager;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private Resources createResources(Context context, AssetManager assetManager) {
        Resources superRes = context.getResources();
        Resources resources = new Resources(assetManager, superRes.getDisplayMetrics(), superRes.getConfiguration());
        return resources;
    }

    public Resources getSkinResource() {
        return mResources;
    }
    
    /**
     * 初始化PackageInfo
     * @param context
     * @return
     */
    public void initPackageInfo(Context context) {
    	mPackageInfo = context.getPackageManager().getPackageArchiveInfo(
    			APK_ABSOLUTE_PATH, PackageManager.GET_ACTIVITIES);
    } 
    public PackageInfo getPackageInfo() {
    	return mPackageInfo;
    }
    /**
     * 
     * @param resName 资源名
     * @param contentName 目录名
     * @return
     */
    public int getId(String resName, String contentName) {
    	//获取资源的id并加载
    	int resId = mResources.getIdentifier( resName, contentName,mPackageInfo.packageName);
    	return resId;
    	/*//加载xml
    	int strId = mResources.getIdentifier( "dynamic_load", "string", packageInfo.packageName);
    	int layoutID = mResources.getIdentifier( "activity_test", "layout", packageInfo.packageName);
 
    	XmlResourceParser xmlResourceParser = mResources.getXml(layoutID);

    	imageView.setImageDrawable(mResources.getDrawable(imageId));

    	textView.setText(mResources.getString(strId));

    	View view= LayoutInflater. from( this).inflate(xmlResourceParser, null);

    	mView.addView(view, 0);

    	} catch(Exception e) {

    	e.printStackTrace();
    	}*/
    }
}
